package com.chenfan.mcn.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.chenfan.common.extension.mybatisplus.assembler.ParameterObjectAssembler;
import com.chenfan.common.extension.mybatisplus.enhanced.EnhancedSqlInjector;
import com.chenfan.common.extension.mybatisplus.interceptor.DefaultTenantIsolationHandler;
import com.chenfan.common.extension.mybatisplus.interceptor.ParameterObjectInnerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Objects;

/**
 * @author lizhejin
 */
@Configuration
@SuppressWarnings("deprecation")
public class MybatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(@Autowired(required = false) ParameterObjectAssembler parameterObjectAssembler) {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new DefaultTenantIsolationHandler()));
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        if (Objects.nonNull(parameterObjectAssembler)) {
            interceptor.addInnerInterceptor(new ParameterObjectInnerInterceptor(parameterObjectAssembler));
        }
        return interceptor;
    }

    /**
     * SQL注入器-方法增强支持
     * @return
     */
    @Bean
    public ISqlInjector sqlInjector(){
        return new EnhancedSqlInjector();
    }
}
